package com.gduf.lsm.service;

import cn.hutool.core.util.ObjectUtil;
import com.gduf.lsm.common.enums.ResultCodeEnum;
import com.gduf.lsm.entity.Serve;
import com.gduf.lsm.exception.CustomException;
import com.gduf.lsm.mapper.ServeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author lsm
 * @Data
 * 养老服务
 */
@Service
public class ServeService {
    @Autowired
    private ServeMapper serveMapper;

    /**
     * 新增
     * @param serve
     */
    public void add(Serve serve) {
        //判断养老服务名是否为空
        if(serve.getName()==null){
            throw new CustomException(ResultCodeEnum.SERVE_NULL_ERROR);
        }
        //判断养老服务名是否重复
        Serve se=serveMapper.selectByName(serve.getName());
        if(ObjectUtil.isNotNull(se)){
            throw new CustomException(ResultCodeEnum.NEWS_EXIST_ERROR);
        }
        serveMapper.insert(serve);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(Integer id) {
        serveMapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            serveMapper.deleteById(id);
        }
    }

    /**
     * 修改
     * @param serve
     */
    public void updateById(Serve serve) {
        serveMapper.updateById(serve);
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Serve selectById(Integer id) {
        return serveMapper.selectById(id);
    }

    /**
     * 查询所有
     * @param serve
     * @return
     */
    public List<Serve> selectAll(Serve serve) {
        return serveMapper.selectAll(serve);
    }

    /**
     * 分页查询
     * @param serve
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<Serve> selectPage(Serve serve, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Serve> list = serveMapper.selectAll(serve);
        return PageInfo.of(list);
    }
}
